package com.xzit.tmms.controller.teacher;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xzit.tmms.bean.unit.Teacher;
import com.xzit.tmms.common.base.BaseController;
import com.xzit.tmms.common.exception.ServiceException;
import com.xzit.tmms.common.page.Page;
import com.xzit.tmms.service.teacher.TeacherService;
import com.xzit.tmms.service.unit.UnitService;


//teacher表的增删改查操作
@Controller
@RequestMapping("/teacher")
public class TeacherController extends BaseController{
	@Resource
	private TeacherService teacherService;
	
	@Resource
	private UnitService unitService;
	
	@RequestMapping("")
	public String index(){
		return "teacher/teacher-index";
	}
	
	@RequestMapping("/queryTeacher")
	public String queryTeacher(Model model) throws ServiceException{
		Page<Map<String,Object>> page = new Page<>(getPageIndex(),getPageSize());
		model.addAttribute("page",teacherService.queryTeacher(page));
		return "teacher/teacher-query";
		
	}
	
	//此操作用来返回Unit表中的数据，做多表关联，将Uint表中的信息传到页面
	@RequestMapping("/addTeacher")
	public String addTeacher(Model model) throws ServiceException{
		model.addAttribute("unitList",unitService.getUnitList());
		return "teacher/teacher-add";
		
	}
	
	//增加教师功能的增加操作，将页面数据全部提交到后台操作
	@RequestMapping("/addTeacherSubmit")
	public String addTeacherSubmit(Teacher teacher) throws ServiceException{
		teacherService.addTeacher(teacher);
		return "redirect:/teacher.action";
	}
	
	@RequestMapping("/deleteTeacher")
	public String deletrTeacher(Integer teacherId) throws ServiceException{
		teacherService.deleteTeacher(teacherId);
		return "forward:queryTeacher.action";
		
	}
	
	
//	与ADD思路一样，此表现将数据封装在Model中一起提交
	@RequestMapping("/updateTeacher")
	public String updateTeacher(Model model,Integer teacherId) throws ServiceException{
		Teacher teacher = teacherService.getTeacherById(teacherId);
		model.addAttribute("teacher",teacher);
		model.addAttribute("unitList",unitService.getUnitList());
		
		return "teacher/teacher-update";
	}
	
	@RequestMapping("/updatecherSubmit")
	public String upteacherSubmit(Model model,Teacher teacher) throws ServiceException{
		teacherService.updateTeahcer(teacher);
		return "redirect:/teacher.action";
		
	}
}
